﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Media3D;
using Newtonsoft.Json.Linq;

namespace CheemsControl.Animations
{
    internal class AnimationHelper
    {
        /// <summary>
        /// 鼠标移入控件自动放大
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="centerX">中心点x</param>
        /// <param name="centerY">中心点y</param>
        /// <param name="enlargeScale">放大比例</param>
        /// <param name="speed">速度，越小越快，表示鼠标进入后动画时间</param>
        internal static void ApplyMouseEnterAnimation(UIElement control,double centerX = 0,double centerY = 0,double enlargeScale = 1.02 ,double speed = 0.2)
        {
            // 创建鼠标进入时的动画
            var scaleUp = new ScaleTransform(1, 1); // 初始缩放比例
            control.RenderTransform = scaleUp;

            // 设置控件的中心
            scaleUp.CenterX = centerX;
            scaleUp.CenterY = centerY;

            // 创建动画: 缩放变大
            var scaleAnimation = new DoubleAnimation
            {
                From = 1,
                To = enlargeScale,
                Duration = new Duration(TimeSpan.FromSeconds(speed))
            };

            // 鼠标移出时的动画：恢复原状
            var scaleDownAnimation = new DoubleAnimation
            {
                From = enlargeScale,
                To = 1,
                Duration = new Duration(TimeSpan.FromSeconds(speed))
            };

            // 为鼠标进入事件添加动画
            control.MouseEnter += (sender, e) =>
            {
                // 设置控件置顶
                Panel.SetZIndex(control, 1000); // 这里1000是一个很大的值，确保控件在最上层

                scaleUp.BeginAnimation(ScaleTransform.ScaleXProperty, scaleAnimation);
                scaleUp.BeginAnimation(ScaleTransform.ScaleYProperty, scaleAnimation);
            };

            // 为鼠标移出事件添加动画
            control.MouseLeave += (sender, e) =>
            {
                // 恢复控件的Z轴索引，回到默认层级
                Panel.SetZIndex(control, 0); // 默认层级，或根据需要调整

                scaleUp.BeginAnimation(ScaleTransform.ScaleXProperty, scaleDownAnimation);
                scaleUp.BeginAnimation(ScaleTransform.ScaleYProperty, scaleDownAnimation);
            };
        }

        /// <summary>
        /// 让控件可以同时拖动和缩放
        /// </summary>
        /// <param name="control"></param>
        /// <exception cref="ArgumentNullException"></exception>
        internal static void EnableDragAndScale(UIElement control)
        {
            if (control == null) throw new ArgumentNullException(nameof(control));

            double width = 0;
            double height = 0;

            if (control is FrameworkElement element)
            {
                width = element.ActualWidth;
                height = element.ActualHeight;
            }

            // 初始化状态记录
            Point initialMousePosition = new Point();
            Point initialElementPosition = new Point();
            double initialScale = 1;

            // 确保控件有 RenderTransform
            if (control.RenderTransform == null || !(control.RenderTransform is TransformGroup))
            {
                control.RenderTransform = new TransformGroup();
                var newTransformGroup = (TransformGroup)control.RenderTransform;  // 使用不同的变量名
                newTransformGroup.Children.Add(new TranslateTransform()); // 添加 TranslateTransform
                newTransformGroup.Children.Add(new ScaleTransform(1, 1)); // 添加 ScaleTransform
            }

            var transformGroup = (TransformGroup)control.RenderTransform;
            var translateTransform = (TranslateTransform)transformGroup.Children[0];
            var scaleTransform = (ScaleTransform)transformGroup.Children[1];

            // 鼠标按下事件：记录初始位置
            control.MouseLeftButtonDown += (sender, args) =>
            {
                initialMousePosition = args.GetPosition(null);
                initialElementPosition = new Point(translateTransform.X, translateTransform.Y);
                control.CaptureMouse();
                args.Handled = true; // 防止事件冒泡到外部容器
            };

            // 鼠标移动事件：计算拖动位置和缩放
            control.MouseMove += (sender, args) =>
            {
                if (control.IsMouseCaptured && args.LeftButton == MouseButtonState.Pressed)
                {
                    // 拖动计算
                    Point currentMousePosition = args.GetPosition(null);
                    double offsetX = currentMousePosition.X - initialMousePosition.X;
                    double offsetY = currentMousePosition.Y - initialMousePosition.Y;

                    // 更新位置
                    double newX = initialElementPosition.X + offsetX / scaleTransform.ScaleX;
                    double newY = initialElementPosition.Y + offsetY / scaleTransform.ScaleY;

                    // 限制拖动范围
                    double controlWidth = width * scaleTransform.ScaleX;
                    double controlHeight = height * scaleTransform.ScaleY;
                    double leftXLimit = (width * 0.5 - controlWidth) / scaleTransform.ScaleX;
                    double leftYLimit = (height * 0.5 - controlHeight) / scaleTransform.ScaleX;
                    double rightXLimit = width * 0.5 / scaleTransform.ScaleX;
                    double rightYLimit = height * 0.5 / scaleTransform.ScaleY;

                    // 如果图片过小，右下角限制就放开
                    if (scaleTransform.ScaleX < 1)
                    {
                        rightXLimit += width * 0.5 * (1 - scaleTransform.ScaleX) / scaleTransform.ScaleX;
                    }
                    if (scaleTransform.ScaleY < 1)
                    {
                        rightYLimit += height * 0.5 * (1 - scaleTransform.ScaleY) / scaleTransform.ScaleY;
                    }
                    newX = Math.Clamp(newX, leftXLimit < 0 ? leftXLimit : 0, rightXLimit);
                    newY = Math.Clamp(newY, leftYLimit < 0 ? leftYLimit : 0, rightYLimit);
                    translateTransform.X = newX;
                    translateTransform.Y = newY;
                }
            };

            // 鼠标释放事件：释放捕获
            control.MouseLeftButtonUp += (sender, args) =>
            {
                if (control.IsMouseCaptured)
                {
                    control.ReleaseMouseCapture();
                }
            };

            // 鼠标滚轮事件：控制缩放
            control.MouseWheel += (sender, args) =>
            {
                if (args.Delta != 0)
                {
                    // 获取鼠标相对控件的位置
                    Point mousePosition = args.GetPosition(control);

                    // 根据鼠标位置计算缩放因子
                    double scaleFactor = args.Delta > 0 ? 1.1 : 0.9; // 放大或缩小因子
                    double _initialScale = initialScale * scaleFactor;

                    // 限制缩放范围，避免无限放大或缩小
                    _initialScale = Math.Clamp(_initialScale, 0.1, 10);

                    if (_initialScale == initialScale)
                    {
                        return;
                    }
                    initialScale = _initialScale;

                    // 计算缩放前的鼠标位置相对于控件的位置
                    double offsetX = mousePosition.X - translateTransform.X;
                    double offsetY = mousePosition.Y - translateTransform.Y;

                    // 更新 ScaleTransform
                    scaleTransform.ScaleX = initialScale;
                    scaleTransform.ScaleY = initialScale;

                    // 计算新的位置，以鼠标为中心
                    translateTransform.X = (translateTransform.X - mousePosition.X * (scaleFactor - 1)) / scaleFactor;
                    translateTransform.Y = (translateTransform.Y - mousePosition.Y * (scaleFactor - 1)) / scaleFactor;

                    args.Handled = true; // 防止事件冒泡
                }
            };
        }

    }
}
